I tried using multiple Intrinsic function together in AWS CloudFormation
I was working with a CloudFormation i want to use multiple intrinsic function together so i tried and shared bellow:
Introduction:
Fn::Sub Function
The Fn::Sub function replaces variables in the input string with specified values. You can use the Fn::Sub function to replace supported functions such as Fn::FindInMap, Fn::ImportValue, or to replace variables in the input string. The syntax for the Fn::Sub function is:
{ "Fn::Sub" : [ String, { Var1Name: Var1Value, Var2Name: Var2Value } ] }
The first parameter is a string containing the variables to be assigned. The second parameter is an object that specifies the name and value of each variable to be assigned. Let's take a look at some examples of using the Fn::Sub function in functions that support it.
Usage of Fn::Sub with Fn::FindInMap function
The Fn::FindInMap function returns the value corresponding to the specified key in the specified map. The Fn::FindInMap function has the following syntax:
{ "Fn::FindInMap" : [ MapName, TopLevelKey, SecondLevelKey ] }
In the following example, the Fn::Sub and Fn::FindInMap functions are used to substitute the variable log_group_name with the value of the mapping result:
{ "Mappings": { "LogGroupMapping": { "Test": { "Name": "test_log_group" }, "Prod": { "Name": "prod_log_group" } } }, "Resources": { "myLogGroup": { "Type": "AWS::Logs::LogGroup", "Properties": { "LogGroupName": { "Fn::Sub": [ "cloud_watch_${log_group_name}", { "log_group_name": { "Fn::FindInMap": [ "LogGroupMapping", "Test", "Name" ] } } ] } } } } }
This example uses the Fn::FindInMap function to find the "Name" value in the "LogGroupMapping" map. Then use the Fn::Sub function to replace the log_group_name variable with the resulting value of the mapping.
Usage of Fn::Sub with Fn::ImportValue function
The Fn::ImportValue function returns the value of an exported output from another CloudFormation stack. The syntax for the Fn::ImportValue function is:
{ "Fn::ImportValue" : "exported-output-name" }
The following example uses the Fn::Sub function with the Fn::ImportValue function to replace the Domain variable with a value derived from the imported value.
{ "Resources": { "DNS": { "Type": "AWS::Route53::HostedZone", "Properties": { "Name": { "Fn::Sub": [ "www.${Domain}", { "Domain": { "Fn::ImportValue": "DomainName" } } ] } } } } } ``
Reference:
https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-fn-sub-function/